package com.jetpack.mvvm.lib.binding.viewbind

import android.app.Activity
import android.os.Build
import androidx.viewbinding.ViewBinding
import com.jetpack.mvvm.lib.binding.base.ActivityDelegate
import com.jetpack.mvvm.lib.binding.ext.inflateMethod
import kotlin.reflect.KProperty

class ActivityViewBinding<T : ViewBinding>(
    clazz: Class<T>, activity: Activity
): ActivityDelegate<T>(activity) {
    private var layoutInflater = clazz.inflateMethod()
    override fun getValue(thisRef: Activity, property: KProperty<*>): T {
        return viewBinding?.run {
            this
        } ?: let {
            // 当继承 Activity 且 Build.VERSION.SDK_INT < Build.VERSION_CODES.Q 时触发
            addLifecycleFragment(thisRef)

            val binding = layoutInflater.invoke(null, thisRef.layoutInflater) as T
            return binding.apply { viewBinding = this }
        }
    }

}